package com.jaya.budan.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.jaya.budan.R;
import com.jaya.budan.adapter.ViewPagerAdapter;
import com.jaya.budan.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

public class SquareFragment extends BaseFragment {

    private TabLayout tab_layout;
    private ViewPager fragment_vp_square;
    private String access_token;
    private List<Fragment> fragmentList;
    private String[] mTitles = new String[]{"推荐", "关注", "视频"};
    private Square_ReCommend_Fragment reCommendFragment;
    private Square_Concer_Fragment concerFragment;
    private Square_Nearby_Fragment nearByFragment;
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_square, container, false);
        fragment_vp_square = view.findViewById(R.id.fragment_vp_square);
        tab_layout = getActivity().findViewById(R.id.tab_layout);
        tab_layout.setVisibility(View.VISIBLE);
        ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity(), getActivity().getSupportFragmentManager());
        adapter.setTitles(mTitles);
        Bundle bundle = getArguments();
        access_token = bundle.getString("access_token");
        fragmentList = new ArrayList<>();
        if (reCommendFragment == null){
            reCommendFragment = new Square_ReCommend_Fragment();
            Bundle bundle2 = new Bundle();
            bundle2.putString("access_token", access_token);
            reCommendFragment.setArguments(bundle2);
            fragmentList.add(reCommendFragment);
        }
        if (concerFragment == null){
            concerFragment = new Square_Concer_Fragment();
            Bundle bundle2 = new Bundle();
            bundle2.putString("access_token", access_token);
            concerFragment.setArguments(bundle2);
            fragmentList.add(concerFragment);
        }
        if (nearByFragment == null){
            nearByFragment = new Square_Nearby_Fragment();
            Bundle bundle2 = new Bundle();
            bundle2.putString("access_token", access_token);
            nearByFragment.setArguments(bundle2);
            fragmentList.add(nearByFragment);
        }
        adapter.setFragments(fragmentList);
        fragment_vp_square.setCurrentItem(0);
        fragment_vp_square.setAdapter(adapter);
        tab_layout.setupWithViewPager(fragment_vp_square);
        fragment_vp_square.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        return view;
    }

    public static SquareFragment newInstance() {
        Bundle args = new Bundle();
        SquareFragment fragment = new SquareFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onResume() {
        super.onResume();
    }
}
